package com.wyd.filter;

import com.wyd.entity.Context;
import com.wyd.entity.DseProductPriorityInfo;
import lombok.extern.slf4j.Slf4j;
import org.jeasy.rules.annotation.Action;
import org.jeasy.rules.annotation.Condition;
import org.jeasy.rules.annotation.Fact;
import org.jeasy.rules.annotation.Rule;

import java.util.Iterator;

@Rule(name = "appoint_mnc_filter", description = "强制匹配运营商过滤", priority = 3)
@Slf4j
public class AppointMncFilter {

  @Condition
  public boolean when() {
    return true;
  }

  @Action
  public void action(@Fact("ct") Context ct) {
    log.info("开始执行>>>强制匹配运营商过滤");

    if(ct.getDseProductList() == null || ct.getDseProductList().isEmpty()){
        return;
    }

    //终端参数包括appointMnc（plmn），则代表需要匹配指定运营商网络
    if(ct.getAppointMnc() != null && ct.getAppointMnc().length() > 0) {
      Iterator<DseProductPriorityInfo> iterator = ct.getDseProductList().iterator();
      while (iterator.hasNext()) {
        DseProductPriorityInfo info = iterator.next();
        if (info.getPlmns().isEmpty() || !info.getPlmns().contains(ct.getAppointMnc())) {
          iterator.remove();
          log.info("强制匹配运营商过滤产品id>>>{},待匹配mnc:{}", info.getDseProductId(),ct.getAppointMnc());
        }
      }
    }
  }
}
